<div class="modal-header">
  <button type="button" class="close" data-dismiss="modal" aria-label="关闭"><span aria-hidden="true">&times;</span></button>
  <h4 class="modal-title" id="common-modal-label">选择负责老师</h4>
</div>
<div class="modal-body">
	<table data-ajax="handlerSearch" 
				 data-method="post" 
			 	 data-id-field="id" 
			 	 data-unique-id="id" 
				 data-pagination="true" 
				 data-check-on-init="true" 
				 data-click-to-select="true" 
				 data-side-pagination="server" 
				 data-mobile-responsive="true" 
    		 data-form-search=".search-form-teacher" class="bootstrap-table table-teacher">
		<thead>
			<tr>
				<th data-checkbox="true"></th>
				<th data-field="id" data-visible="false">ID</th>
				<th data-field="name" 			data-align="center" >姓名</th>
				<th data-field="sex" 				data-align="center" >性别</th>
				<th data-field="mobile" 		data-align="center" >手机</th>
				<th data-field="duty" 			data-align="center" >职务</th>
				<th data-field="classes" 		data-align="center" data-formatter="renderClasses">负责班级数</th>
			</tr>
		</thead>
	</table>
</div>
<div class="modal-footer">
  <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
  <button type="button" class="btn btn-primary" onclick="handlerConfirm()">确认</button>
</div>

<!-- 查询 表单 ========================================================= -->
<form class="search-form-teacher" action="/miwawa/{dbname}/common/search.json" method="post" 
			name="com.app.miwawa.api.eam.entity.view.TeacherView">
			<input type="hidden" name="status" value="0" data-search-group="and" data-search-operation="EQ">
			<input type="hidden" name="invalid" value="F" data-search-group="and" data-search-operation="EQ">
			<input type="hidden" data-search-sort="createDate" data-search-order="desc">
</form>

<script type="text/javascript">
<!--
	// 打开老师选择模态框后回调
	function onTeacherShow(){
		if(!jQuery(".table-teacher").hasClass("table")){
			jQuery(".table-teacher").bootstrapTable();
		}
	}
	
	// 确认选择老师
	function handlerConfirm(){
		var rows = jQuery(".table-teacher").bootstrapTable("getSelections");
		if(rows.length == 0){
			jQuery.message.warning("您还未选择该班级的负责或助教老师！");
		}else{
			var teachers = [],names = [],teacherName=[];
			jQuery.each(rows,function(index,row){
				var teacher = jQuery("#table-teacher").bootstrapTable("getRowByUniqueId", row.id);
				if(teacher){
					names.push(teacher.name);
				}else{
					teachers.push({
						"target":"teacher",
						"targetid":row.id,
						"classesid":classes.id,
						"status":"0",
						"invalid":"F"
					});
					jQuery.ajaxsend({
						url:"/miwawa/{dbname}/common/search.json".replace("{dbname}",dbname),
						async:false,
						data:{
				       		"entityClass" : "com.app.miwawa.api.eam.entity.view.ClassesTeacher",
				          "and[0][operator]": "EQ",
				          "and[0][fieldName]": "teacherid",					
				          "and[0][fieldValue]": row.id
						},
						success:function(data){
							var list = data.rows;
							for(var i=0; i<list.length; i++){
								var classesName = list[i].classes;
								var week = classesName.substring(0,2);
								var time = $moment("2016-09-14 "+classesName.substring(2,7)+":00");
								var lessonWeek = jQuery("#lessonWeek").val();
								if( week == lessonWeek && ($moment("2016-09-14 "+jQuery("#lessonTime").val()+":00").add(45,"m").isAfter(time) && time.isAfter($moment("2016-09-14 "+jQuery("#lessonTime").val()+":00").add(-45,"m"))) ){									
									teacherName.push(row.name);
						  		teachers = [];
						  		break;
						  	}
							}
						}
					});
				}
			});
			
			if(names.length > 0){
				jQuery.message.warning("您选择的“"+names.join("、")+"”等老师，已是本班负责老师！");
			}
			
			if(teacherName.length > 0){
				jQuery.message.alert("您选择的 "+teacherName.join("、")+" 等老师，在该班级上课时间内，在其他班级上课，无法分到该班级！");
			}
			
			if(teachers.length > 0){
				jQuery.ajaxsend({
					url:"/miwawa/{dbname}/common/save".replace("{dbname}",dbname),
					data:{
						"entityClass":"com.app.miwawa.api.eam.entity.ClassesRelation",
						"beans":teachers
					},
					success:function(data){
						jQuery("#table-teacher").bootstrapTable('refresh');
					}
				});
			}
			
			jQuery(".table-teacher").bootstrapTable('uncheckAll');
			jQuery('#common-modal').modal('hide');
		}
	}
	
	// 渲染老师选择列表的班级数
	function renderClasses(value, row, index){
		return '<a href="javascript:void(0);"><span class="badge bg-orange">'+value+'</span></a>';
	}
//-->
</script>